<template>
  <div>
    <br/> <br/>
    <el-row :gutter="15">
      <el-form ref="taxiInvoice" :model="formData" :rules="rules" size="medium" label-width="100px">
        <el-col :span="24">
          <el-form-item label-width="140px" label="上传出租车发票" prop="importFile">
            <el-upload ref="import" :file-list="importfileList" :http-request="ocrUpload" :show-file-list="false"
              :before-upload="importBeforeUpload">
              <el-button size="small" type="primary" icon="el-icon-upload">点击识别</el-button>
            </el-upload>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="发票代码" prop="InvoiceCode">
            <el-input v-model="formData.InvoiceCode" placeholder="请输入发票代码" :maxlength="16" show-word-limit
              clearable :style="{width: '100%'}"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="发票号码" prop="InvoiceNum">
            <el-input v-model="formData.InvoiceNum" placeholder="请输入发票号码" :maxlength="10" show-word-limit
              clearable :style="{width: '100%'}"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="车牌号" prop="TaxiNum">
            <el-input v-model="formData.TaxiNum" placeholder="请输入车牌号" clearable prefix-icon='el-icon-truck'
              :style="{width: '100%'}"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="乘车日期" prop="Date">
            <el-date-picker v-model="formData.Date" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
              :style="{width: '100%'}" placeholder="请选择乘车日期" clearable></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="金额" prop="TotalFare">
            <el-input v-model="formData.TotalFare" placeholder="请输入金额" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="省份" prop="province">
            <el-input v-model="formData.Province" placeholder="请输入省份" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item size="large">
            <el-button type="primary" @click="submitForm">提交</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
  </div>
</template>
<script>
import {ocrIdentifyWithTaxi} from "@/api/invoice/invoice";

export default {
  components: {},
  props: [],
  data() {
    return {
      formData: {
        Import: null,
        InvoiceCode: '',
        InvoiceNum: '',
        TaxiNum: undefined,
        Time: null,
        TotalFare: undefined,
        Province: undefined,
      },
      rules: {
        InvoiceCode: [],
        InvoiceNum: [],
        TaxiNum: [{
          required: true,
          message: '请输入车牌号',
          trigger: 'blur'
        }],
        Time: [{
          required: true,
          message: '请选择乘车日期',
          trigger: 'change'
        }],
        TotalFare: [{
          required: true,
          message: '请输入金额',
          trigger: 'blur'
        }],
        Province: [],
      },
      importfileList: [],
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    submitForm() {
      this.$refs['taxiInvoice'].validate(valid => {
        if (!valid) return
        // TODO 提交表单
      })
    },
    resetForm() {
      this.$refs['taxiInvoice'].resetFields()
    },
    importBeforeUpload(file) {
      let isRightSize = file.size / 1024 / 1024 < 2
      if (!isRightSize) {
        this.$message.error('文件大小超过 2MB')
      }
      return isRightSize
    },
    ocrUpload(options) {
      const formData = new FormData();
      formData.append('file', options.file);

      ocrIdentifyWithTaxi(formData).then(response => {
        this.formData = response.data.words_result;
      }).catch(() => {
        alert('ocr识别失败')
      })
    },
  }
}

</script>
<style>
.el-upload__tip {
  line-height: 1.2;
}

</style>
